﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MageWars.Support
{
    public static class Collisions
    {
        static Collisions()
        {
            MapData = new Color[Textures.CollisionMap.Width * Textures.CollisionMap.Height];
            Textures.CollisionMap.GetData(MapData);
            TexturesData = new Dictionary<Texture2D, Color[]>();
        }


        private static Dictionary<Texture2D, Color[]> TexturesData;
        private static Color[] MapData;

        public static bool CollidedWithMap(Vector3 SpritePosition, Texture2D SpriteTexture)
        {
            Rectangle SpriteRectangle = new Rectangle((int)SpritePosition.X, (int)SpritePosition.Y, SpriteTexture.Width, SpriteTexture.Height);

            if (!TexturesData.ContainsKey(SpriteTexture))
            {
                var sd = new Color[SpriteTexture.Width * SpriteTexture.Height];
                SpriteTexture.GetData(sd);
                TexturesData.Add(SpriteTexture, sd);
            }

            Color[] SpriteData = TexturesData[SpriteTexture];

            for (int i = SpriteRectangle.Top; i < SpriteRectangle.Bottom; i++)
            {
                for (int j = SpriteRectangle.Left; j < SpriteRectangle.Right; j++)
                {
                    var SpriteDataIndex = (i - SpriteRectangle.Top) * SpriteTexture.Width + (j - SpriteRectangle.Left);
                    var MapDataIndex = i * Textures.CollisionMap.Width + j;
                    if (SpriteData[SpriteDataIndex] != Color.Transparent)
                    {
                        if (MapDataIndex < 0 || MapData[MapDataIndex] != Color.White)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }



        public static bool RectanguloCubreUnSoloColor(Rectangle SpriteRectangle)
        {
            Color CurrentColor;
            Color OldColor = Color.Transparent;
            for (int i = SpriteRectangle.Top; i < SpriteRectangle.Bottom; i++)
            {
                for (int j = SpriteRectangle.Left; j < SpriteRectangle.Right; j++)
                {
                    var MapDataIndex = i * Textures.CollisionMap.Width + j;
                    CurrentColor = MapData[MapDataIndex];
                    if (OldColor != Color.Transparent && OldColor != CurrentColor)



                    {
                        return false;
                    }
                    else if (OldColor == Color.Transparent)
                    {
                        OldColor = CurrentColor;
                    }
                }
            }


            return true;
        }

        public static bool RectanguloCubreNegro(Rectangle SpriteRectangle)
        {
            Color CurrentColor;
            Color OldColor = Color.Transparent;
            for (int i = SpriteRectangle.Top; i < SpriteRectangle.Bottom; i++)
            {
                for (int j = SpriteRectangle.Left; j < SpriteRectangle.Right; j++)
                {
                    var MapDataIndex = i * Textures.CollisionMap.Width + j;
                    CurrentColor = MapData[MapDataIndex];
                    if (CurrentColor == Color.Black)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
